package com.rc.evcharger.service.member;

import com.rc.evcharger.common.constant.SocketConstant;
import com.rc.evcharger.mapper.elec.ElecStationMapper;
import com.rc.evcharger.mapper.enterprise.EnterpriseMemberMapper;
import com.rc.evcharger.mapper.enterprise.EnterpriseSubmemberShareMapper;
import com.rc.evcharger.mapper.member.MemberAccountMapper;
import com.rc.evcharger.mapper.member.MemberCardMapper;
import com.rc.evcharger.mapper.member.MemberInfoMapper;
import com.rc.evcharger.mapper.order.OrderInfoMapper;
import com.rc.evcharger.mapper.rc.RcMemberFlagMapper;
import com.rc.evcharger.model.elec.ElecStation;
import com.rc.evcharger.model.enterprise.EnterpriseMember;
import com.rc.evcharger.model.enterprise.EnterpriseSubmemberShare;
import com.rc.evcharger.model.member.*;
import com.rc.evcharger.model.order.OrderInfo;
import com.rc.evcharger.model.rc.RcMemberFlag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by sven on 2019/09/23.
 */
@Service
public class MemberInfoServiceImpl implements MemberInfoService {

    @Autowired
    private MemberInfoMapper memberInfoMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private MemberCardMapper memberCardMapper;

    @Autowired
    private MemberAccountMapper memberAccountMapper;

    @Autowired
    private ElecStationMapper elecStationMapper;

    @Autowired
    private EnterpriseSubmemberShareMapper enterpriseSubmemberShareMapper;

    @Autowired
    private RcMemberFlagMapper rcMemberFlagMapper;

    @Autowired
    private EnterpriseMemberMapper enterpriseMemberMapper;

    @Override
    public MemberInfoMapper getMapper() {
        return memberInfoMapper;
    }

    /**
     * /支付的账户类型 0个人钱包 1企业钱包 2共享余额 默认0
     **/
    @Override
    public BigDecimal getAccountByChargeType(Long orderId) {
        OrderInfo order = orderInfoMapper.selectByPrimaryKey(orderId);
        MemberInfo memberInfo = memberInfoMapper.selectByPrimaryKey(order.getMemberId());

        MemberAccount memberAccount = memberAccountMapper.selectByPrimaryKey(memberInfo.getMemberId());

        //支付的账户类型 0个人钱包 1企业钱包 2共享余额 3先付后退钱包 4.支付分钱包 默认0
        if ((SocketConstant.STR_CON1).equals(order.getAccountType())) {
            RcMemberFlag rcMemberFlag = rcMemberFlagMapper.selectByPrimaryKey(memberInfo.getMemberId());
            EnterpriseMember enterpriseMember = enterpriseMemberMapper.selectByPrimaryKey(rcMemberFlag.getEnterpriseId());
            //企业余额加授信剩余额度
            BigDecimal money = enterpriseMember.getEnterpriseAccount().add(enterpriseMember.getRemainCreditAmount());
            return money;
        } else if ("0".equals(order.getAccountType())) {
            return memberAccount.getAccount();
        } else {
            return memberAccount.getPaymentRefundAccount();
        }
    }

    @Override
    public MemberInfo selectCardNo(String cardNo, String tenantCode) {

        MemberCardExample memberCardExample = new MemberCardExample();
        memberCardExample.createCriteria().andCardNoEqualTo(cardNo);

        List<MemberCard> memberCards = memberCardMapper.selectByExample(memberCardExample);
        if (CollectionUtils.isEmpty(memberCards)) {
            return null;
        }

        return memberInfoMapper.selectByPrimaryKey(memberCards.get(0).getMemberId());
    }

    @Override
    public int selectAccountType(Long memberId) {

        MemberInfo memberInfo = memberInfoMapper.selectByPrimaryKey(memberId);

        MemberCardExample memberCardExample = new MemberCardExample();
        memberCardExample.createCriteria().andMemberIdEqualTo(memberId);

        List<MemberCard> memberCards = memberCardMapper.selectByExample(memberCardExample);
        if (CollectionUtils.isEmpty(memberCards)) {
            return 0;
        }

        return 0;
    }

    @Override
    public MemberInfo selectEnterpriseMemberByMemberId(Long memberId) {
        return memberInfoMapper.selectEnterpriseMemberByMemberId(memberId);
    }

    @Override
    public BigDecimal selectPowerLimitByMemberId(String tenantCode, Long memberId) {
        EnterpriseSubmemberShare enterpriseSubmemberShare = enterpriseSubmemberShareMapper.selectByTenantCodeAndMemberId(tenantCode, memberId);

        if (enterpriseSubmemberShare == null) {
            return BigDecimal.ZERO;
        }
        return enterpriseSubmemberShare.getFixedPower().subtract(enterpriseSubmemberShare.getSharePower());
    }

    @Override
    public void updateMemberInfoStatus(Long memberId) {
        MemberInfo memberInfo = memberInfoMapper.selectByPrimaryKey(memberId);
        //更新用户状态，解锁充电状态
        MemberInfo updateMember = new MemberInfo();
        updateMember.setMemberId(memberInfo.getMemberId());
        updateMember.setChargeStatus(SocketConstant.ELEC_NOT + "");
        //公交车账号不改变预设SOC
        if (63761L != updateMember.getMemberId() || 215657L != updateMember.getMemberId()) {
            updateMember.setLimitSoc(98);    //每次充完电后预设soc设置为95%
        }
        memberInfoMapper.updateByPrimaryKeySelective(updateMember);
    }

    /**
     * 比较会员本身soc与站点soc大小，取小值
     *
     * @param memberId
     * @param elecStationId
     * @return
     */
    @Override
    public int compareLimitSoc(Long memberId, Long elecStationId) {

        MemberInfo memberInfo = memberInfoMapper.selectByPrimaryKey(memberId);
        ElecStation elecStation = elecStationMapper.selectByPrimaryKey(elecStationId);

        if (memberInfo.getLimitSoc() == null && elecStation.getLimitSoc() == null) {
            return SocketConstant.DEFAULT_LIMIT_SOC;
        }

        if (memberInfo.getLimitSoc() != null && elecStation.getLimitSoc() == null) {
            return memberInfo.getLimitSoc();
        }

        if (memberInfo.getLimitSoc() == null && elecStation.getLimitSoc() != null) {
            return elecStation.getLimitSoc();
        }

        if (memberInfo.getLimitSoc().intValue() > elecStation.getLimitSoc()) {
            return elecStation.getLimitSoc();
        } else {
            return memberInfo.getLimitSoc();
        }
    }
}
